package com.bort.system.controller.client;

import com.bort.system.common.DataContainer;
import com.bort.system.info.CheckOutResult;
import com.bort.system.info.DeskInfo;
import com.bort.system.model.TGameLog;
import com.bort.system.service.TDeskService;
import com.bort.system.utils.SpringContextUtils;
import de.felixroske.jfxsupport.FXMLController;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.math.BigDecimal;
import java.net.URL;
import java.util.*;

@FXMLController
public class CloseDialogController implements Initializable {

    TDeskService deskService;

    DeskInfo data;

    List<Integer> hours = null;
    List<Integer> minutes = null;

    private Date closedTime;

    @FXML
    TextField orgTime;

    @FXML
    TextField amount;

    @FXML
    TextField realTime;

    @FXML
    TextField income;

    @FXML
    TextField leftTime;

    @FXML
    TextField leftAmount;

    @FXML
    AnchorPane anchor;

    @FXML
    ComboBox<Integer> hour;

    @FXML
    ComboBox<Integer> minute;

    @FXML
    Button overtimeBtn;

    @FXML
    void onCancelBtnClick(ActionEvent event){
        Scene scene = anchor.getScene();
        Stage window = (Stage)scene.getWindow();
        window.close();
    }

    @FXML
    void onConfirmBtnClick(ActionEvent event){
        BigDecimal pay = new BigDecimal(income.getText());
        boolean end = deskService.closeDesk(data,closedTime,pay);
        if(end){
            DeskController deskCtrl = closeDialog();
            deskCtrl.closeDesk();
        }
    }


    @FXML
    void onConfirmOvertimeBtnClick(ActionEvent event){
        Integer value = hour.getValue();
        Integer mValue = minute.getValue();
        TGameLog log = deskService.addOvertime(data, value, mValue);
        if(null != log){
            DeskController descCtrl = closeDialog();
            data.setEndTime(log.getEndTime());
            descCtrl.overtimeClose();
        }
    }


    @FXML
    void onHourSelect(ActionEvent event){
        updateBtnState();
    }

    @FXML
    void onMinuteSelect(ActionEvent event){
        updateBtnState();
    }

    private DeskController closeDialog() {
        Scene scene = anchor.getScene();
        Stage window = (Stage)scene.getWindow();
        window.close();
        DeskController deskCtrl = DataContainer.deskCtrlMap.get(data.getId());
        return deskCtrl;
    }

    void updateBtnState(){
        Integer zero = new Integer(0);
        Integer value = hour.getValue();
        Integer mValue = minute.getValue();
        if(zero.equals(value) && zero.equals(mValue)){
            overtimeBtn.setDisable(true);
        }else{
            overtimeBtn.setDisable(false);
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Integer one = 1;
        closedTime = new Date();
        data = DataContainer.deskContainer.get(DataContainer.selectDeskId);
        deskService = SpringContextUtils.getBean(TDeskService.class);
        if( one.equals(data.getTimeLimit())){
            CheckOutResult result = deskService.checkOut(data.getStartTime(), data.getEndTime(),data.getPrice());
            orgTime.setText(result.getTotalTime());
            amount.setText(result.getTotalMoney().toString());

            CheckOutResult playResult = deskService.checkOut(data.getStartTime(), new Date(),data.getPrice());
            realTime.setText(playResult.getTotalTime());
            if (null != income){
                income.setText(playResult.getTotalMoney().toString());
            }

            CheckOutResult leftResult = checkOutLeft(result, playResult);
            leftTime.setText(leftResult.getTotalTime());
            if(null != leftAmount){
                leftAmount.setText(leftResult.getTotalMoney().toString());
            }
        } else{
            CheckOutResult playResult = deskService.checkOut(data.getStartTime(), new Date(),data.getPrice());
            realTime.setText(playResult.getTotalTime());
            income.setText(playResult.getTotalMoney().toString());
        }

        if(hour != null){
            hours = new ArrayList<>();
            for(int i = 0;i<25;i++){
                hours.add(i);
            }
            ObservableList<Integer> options = FXCollections.observableList(hours);
            hour.setItems(options);
        }

        if(minute != null){
            minutes = new ArrayList<>();
            for (int i=0;i<60;i++){
                minutes.add(i);
            }
            ObservableList<Integer> minOptions = FXCollections.observableList(minutes);
            minute.setItems(minOptions);
            hour.setValue(1);
            minute.setValue(0);
        }

    }

    private CheckOutResult checkOutLeft(CheckOutResult result, CheckOutResult playResult) {
        BigDecimal orgMinutes = result.getHour().multiply(new BigDecimal(60)).add(result.getMinute());
        BigDecimal playMinutes = playResult.getHour().multiply(new BigDecimal(60)).add(playResult.getMinute());
        BigDecimal minutes = orgMinutes.subtract(playMinutes);
        BigDecimal leftAmount = result.getTotalMoney().subtract(playResult.getTotalMoney());
        BigDecimal hours = minutes.divide(new BigDecimal(60),0,BigDecimal.ROUND_DOWN);
        BigDecimal showMinutes = minutes.subtract(hours.multiply(new BigDecimal(60)));
        return new CheckOutResult(leftAmount,hours,showMinutes);
    }

}
